package org.jks.service;

import java.util.ArrayList;
import java.util.List;

import org.jks.model.User;
import org.springframework.stereotype.Service;

@Service
public class UserService extends EntityService<User>{
	
	public UserService(){
		MODEL_NAME = User.class.getSimpleName();
		CLASS_NAME = User.class;
	}

	@Override
	public List<User> all() {
		return find("from " + MODEL_NAME + " where Username!='admin'");
	}
	
	public User login(String username, String  password) {
		String hql = "from " + MODEL_NAME + " where Username='" + username + "' and Password='" + password + "'";
		List<User> temp = super.find(hql);
		if(temp == null || temp.size() == 0)
		{
			return null;
		}
		return temp.get(0);
	}
	
	public User findByName(String username)
	{
		String hql = "from " + MODEL_NAME + " where Username='" + username + "'";
		List<User> temp = super.find(hql);
		if(temp == null || temp.size() == 0)
		{
			return null;
		}
		return temp.get(0);
	}
	
	public List<User> findByDepartment(long departmentId){
		List<Object> params = new ArrayList<Object>();
		params.add(departmentId);
		return find("from " + MODEL_NAME + " where Department.Id=?", params);
	}
	
	public List<User> findByDptAndFlight(long dptId, long flightId){
		List<Object> params = new ArrayList<Object>();
		params.add(dptId);
		params.add(flightId);
		return find("from " + MODEL_NAME + " where Department.Id=? and Flight.Id=?", params);
	}
}
